<?php

namespace App\Admin\Actions\Grid\Customer;

use App\Models\Customer;
use App\Services\DingSendMessageService;
use App\Services\SaleService;
use Dcat\Admin\Actions\Response;
use Dcat\Admin\Grid\RowAction;

class AutoAllocate extends RowAction
{
    /**
     * @return string
     */
    protected $title = '自动分配';

    /**
     * Handle the action request.
     *
     *
     * @return Response
     */
    public function handle(): Response
    {
        app(SaleService::class)->allocate(true, $this->getKey());
        //钉钉通知
        app(DingSendMessageService::class)->allocateCustomer(Customer::query()->where('id', $this->getKey())->value('return_user_id'));
        return $this->response()
            ->success(trans('admin.processed_successfully'))
            ->refresh();
    }

    /**
     * @return array
     */
    public function confirm(): array
    {
        return ['确定执行自动分配吗', '客户将被分配至复诊部'];
    }

    public function html(): string
    {
        $this->defaultHtmlAttribute('href', 'javascript:void(0)');

        return <<<HTML
<a {$this->formatHtmlAttributes()}><i class="feather icon-repeat"> {$this->title()}</i></a>
HTML;
    }

}
